
// Definition for a binary tree node.
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
var sortedArrayToBST = function(nums) {
  function createBSTTree(low, high) {
    if(low > high)
      return null;
    let mid = Math.floor((low + high) / 2);
    let node = new TreeNode(nums[mid], null, null);
    node.left = createBSTTree(low, mid - 1);
    node.right = createBSTTree(mid + 1, high);
    return node;
  }
  if(nums.length == 0)
    return null;
  return createBSTTree(0, nums.length - 1);
};

let nums = [-10,-3,0,5,9];
console.log(sortedArrayToBST(nums));
nums = [1,3];
console.log(sortedArrayToBST(nums));
